import 'package:boss/models/takeout_order_list_data.dart';
import 'package:boss/ui/page/takeoutManagement/page/takeoutList/provider/takeout_list_provider.dart';
import 'package:boss/ui/widget/calendar_bar.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'takeout_aggregate_data.dart';
import 'takeout_list_item.dart';

class TakeoutListBrowser extends StatefulWidget {
  @override
  _TakeoutListBrowserState createState() => _TakeoutListBrowserState();
}

class _TakeoutListBrowserState extends State<TakeoutListBrowser> {
  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<TakeoutListProvider>(
      context,
      listen: false,
    );
    return Column(
      children: <Widget>[
        Container(
          padding: EdgeInsets.all(30.w),
          child: Consumer<TakeoutListProvider>(
            builder: (context, provider, _) {
              return CalendarBar(
                startDateTime: provider.startTime,
                endDateTime: provider.endTime,
                onDateTimeChanged: provider.setStartTime,
              );
            },
          ),
        ),
        Expanded(
          child: LoaderPullRefresh<TakeoutOrderItem>(
            key: provider.loaderPullRefreshKey,
            loadTask: provider.loadData,
            refreshTask: provider.refreshData,
            builder: (context) => SingleChildScrollView(
              padding: EdgeInsets.only(
                bottom: 30.w
              ),
              child: Column(
                children: <Widget>[
                  Consumer<TakeoutListProvider>(
                    builder: (context, provider, child) {
                      return TakeoutAggregateData(
                        data: provider.takeoutSummary,
                      );
                    },
                  ),
                  Consumer<TakeoutListProvider>(
                    builder: (context, provider, child) {
                      return Column(
                        children: provider.orders.map((item) => TakeoutListItem(data: item)).toList(),
                      );
                    },
                  ),
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }
}
